package com.chuangke.admin.data.builder;

import cn.hutool.core.util.StrUtil;
import com.chuangke.admin.entity.SysDict;
import com.chuangke.admin.service.SysDictService;
import com.chuangke.common.data.builder.BuilderConstant;
import com.chuangke.common.data.builder.DataBuildConfig;
import com.chuangke.common.data.builder.IDataBuilder;
import com.chuangke.common.utils.SpringContextUtil;
import org.apache.commons.beanutils.BeanUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class DictDataBuilder implements IDataBuilder<SysDict> {

	private static final Logger logger = LoggerFactory.getLogger(DictDataBuilder.class);

	public static final String NAME = "sys-dict-builder";

	private String type;

	private String dataType;
	
	public DictDataBuilder() {}

	public DictDataBuilder(String type) {
		this.type = type;
		this.dataType = BuilderConstant.DATA_TYPE_BEAN;
	}

	public DictDataBuilder(String type, String dataType) {
		this.type = type;
		this.dataType = dataType;
	}

	@Override
	public String getName() {
		return NAME+"-"+type;
	}

	@Override
	public Map<String, SysDict> getMetaData() {
		if (StrUtil.isBlank(type)) {
			return new HashMap<>();
		}
		SysDictService sysUserService = SpringContextUtil.getBean(SysDictService.class);
		return sysUserService.findAll().get(type) == null ? new HashMap<>()
				: sysUserService.findAll().get(type).stream().collect(Collectors.toMap(SysDict::getCode, a -> a));
	}

	@Override
	public void build(Object obj, Map<String, SysDict> metaData, List<DataBuildConfig> dataBuildConfigList) {
		if (metaData == null)
			return;
		try {
			for (DataBuildConfig dataBuildBean : dataBuildConfigList) {
				if (BuilderConstant.DATA_TYPE_BEAN.equals(dataType)) {
					String idValue = BeanUtils.getProperty(obj, dataBuildBean.getIdColumn());
					if (StrUtil.isBlank(idValue)) {
						continue;
					}
					List<String> ids = dataBuildBean.isMultiple() ? Arrays.asList(idValue.split(dataBuildBean.getSeparator())) : Arrays.asList(idValue);
					
					
					List<String> names = ids.stream().map(id -> {
						SysDict dict = metaData.get(id);
						return getName(dataBuildBean,dict);
					}).collect(Collectors.toList()) ;
					BeanUtils.setProperty(obj, dataBuildBean.getNameColumn(), String.join(";", names));
				} else {
					@SuppressWarnings("unchecked")
					Map<String, Object> objMap = (Map<String, Object>) obj;
					String id = objMap.get(dataBuildBean.getIdColumn()).toString();
					if (StrUtil.isBlank(id)) {
						continue;
					}
					SysDict dict = metaData.get(id);
					String value = getName(dataBuildBean,dict);
                    objMap.put(dataBuildBean.getNameColumn(), value);
				}
			}
		} catch (Exception e) {
			logger.error("设置字段信息出错", e);
		}
	}

	private String getName(DataBuildConfig config, SysDict dict){
		if(dict == null){
			return "";
		}
		if(StrUtil.isBlank(config.getDictNameColumn())){
			return dict.getName();
		}else{
			return dict.getFullName() ;
		}
	}

}